#include <stdio.h>

int arr[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
	int day = 0;
	int year = 0;
	int month = 0;
	int i = 0;
	int j = 0;
int get_day(int year, int month, int day)
{
	if(month == 1 | month == 2)
		{
			month += 12;
			--year;
		}		
	int week = -1;
	week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 -year / 100 +year / 400) % 7 + 1;
	return week;	// 输出-1为错误
}

void singleMonth()
{
	int day = 0;
	int year = 0;
	int month = 0;
	int i = 0;
	int j = 0;

	printf("请输入查询的年.月 >");
	scanf("%d.%d", &year, &month);
	int week_day = get_day(year, month, 1);
	printf("\tMon\tTus\tWed\tThu\tFri\tSat\tSun\n");
	for(i = 0; i < week_day; i++)
	{
		printf("\t");
	}
	if(year % 400 == 0)
	{
		arr[1] = 29;
		day = arr[month - 1];
	}
	else
	{
		day = arr[month - 1];
	}
	for(i=0,j=week_day; i < day; i++,j++)
	{
        printf("%d\t",i+1);
        if(j>=7)
		{
            printf("\n\t");
            j=0;
        }
    }
	putchar(10);
}

void wholeYear()
{
	int k = 0;
	month = 1;
	printf("请输入查询的年 >");
	scanf("%d", &year);
	//int week_day = get_day(year, month, 1);
	for(k = 0; k < 13; k++)
	{
	printf("\n%d月\n",month);		
	int week_day = get_day(year, month, 1);
	printf("\tMon\tTus\tWed\tThu\tFri\tSat\tSun\n");
	for(i = 0; i < week_day; i++)
	{
		printf("\t");
	}
	if(year % 400 == 0)
	{
		arr[1] = 29;
		day = arr[month - 1];
	}
	else
	{
		day = arr[month - 1];
	}
	for(i=0,j=week_day; i < day; i++,j++)
	{
        printf("%d\t",i+1);
        if(j>=7)
		{
            printf("\n\t");
            j=0;
        }
    }
	}
	putchar(10);
	month++;

}
int main(int argc, const char *argv[])
{
	int input = 0;
	do
	{
		printf("----------查询单月(1)\t查询整年(2)\t退出(0)----------\n");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			singleMonth();
			break;
		case 2:
			wholeYear();
			break;
		case 0:
			printf("退出！\n");
			break;
		default:
			printf("输入有误，请重新输入！\n");
			break;
		}
	}while(input);

	return 0;
}
